﻿<?php


class SxApp extends BaseApp
{
    function index()
    {
		
	    $TB_sxs = &$this->_model('sxs');		
		$sxdata = $TB_sxs->show();
		
		$this->assign('sxdata', $sxdata);
					    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('sx.view.php');
				
    }


    function dlist($sid=0)
    {
		if(!isset($_GET['id']))
		{
		$_GET['id']='';
		}
		$id = $_GET['id'];
	    if(empty($id))
		{
		$id = $sid;	
		}
	 	$sxtempdata = $this->get_sx_cache($id);
		$sxdata = $sxtempdata['data'];		   
        $this->assign('sxdata', $sxdata);
		
        $this->assign('id', $id);			    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('sx_dlist.view.php');
    }

    function sadd()
    {
						    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('sx_sadd.view.php');
    }

	
    function saddpost()
    {
		 $data = $_POST['data'];
		
		 $TB_sxs = &$this->_model('sxs'); 	 
		
		 $id = $TB_sxs->post($data);
		 
		 
         $this->write_cache($id);          	 
         $this->index();		

    }

	
    function daddpost()
    {
		 $data = $_POST['data'];
	
		 $TB_sxd = &$this->_model('sxd'); 	 
		
		 $TB_sxd->post($data);
         $this->write_cache($data['sort']);  	 
         $this->dlist($data['sort']);		

    }

	
    function deditpost()
    {  
	     $did = $_GET['did'];
	     $sid = $_GET['sid'];
	     $v = $_GET['v'];
		 
         $data[name] = $v;		 		 
		 $TB_sxd = &$this->_model('sxd'); 	 
		
		 $TB_sxd->editaction($did,$data);	
         $this->write_cache($sid); 		 	
		 $this->dlist($sid);
    }

    function sedit()
    {
		$id = $_GET['id'];
		 $TB_sxs = &$this->_model('sxs'); 	 
		
		$data = $TB_sxs->oneshow($id);

		   

	    $this->assign('data', $data);		
			
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('sx_sedit.view.php');		

    }
	
    function seditpost()
    {
	     $data = $_POST['data'];

		 
		 $id = intval($data['id']);	 
		 

		 

		 $TB_sxs = &$this->_model('sxs'); 	 	 
		
		 $TB_sxs->editaction($id,$data);
		 
         $this->write_cache($id); 
		 $this->index();		

    }

    function sdel()
    {
	     $id = $_GET['id'];
		 $TB_sxs = &$this->_model('sxs'); 	 
		
		 $TB_sxs->del($id); 

		$pic_dir1 = ROOT_PATH.'/data/sx/sx_'.$id.'.php';
		if(!is_dir($pic_dir)&&file_exists($pic_dir))
		{
		    del_file($pic_dir);	
		}		 
				 

		 $this->index();			

    }
	
    function ddel()
    {
	     $did = $_GET['did'];
	     $sid = $_GET['sid'];		 
		 $TB_sxd = &$this->_model('sxd'); 	 
		
		 $TB_sxd->del($did); 
         $this->write_cache($sid); 		 

		 $this->dlist($sid);		

    }

    function get_sx_cache($id)
    {

        $data = include(ROOT_PATH.'/data/sx/sx_'.$id.'.php');  
		return $data;
    }


    function write_cache($id)
    {
	    $TB_sxd = &$this->_model('sxd');		
		$tempdata = $TB_sxd->show($id);	
		$TB_sxs = &$this->_model('sxs');		
		$tempdata1 = $TB_sxs->oneshow($id);	
		$data = array();
		$data['name']= $tempdata1['sx_name'];	
		$data['data'] = $this->get_list_category($tempdata);
        $file = ROOT_PATH.'/data/sx/sx_'.$id.'.php';
	    write_file($file, "<?php \nreturn " . var_export($data , true) . ";\n?>");
		
		if(!is_writable($file))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }


    }

      function getnbsp($i) 
	  {
	        $str = '';
	        if($i) 
			{
		         for($j=0; $j<$i; $j++)
				 {
			      $str .= "　";
		         }
	        }
	        return $str;
     }

     function get_list_category($array,$parentid=0,$level=0,$add=2) 
	 {
	        $str_pad = $this->getnbsp($level);
	        $newarray = array();
	        $temp = array();
	        foreach($array as $v) 
			{
		       if($v['parent_id'] == $parentid) 
			   {
			       $newarray[] = array(
				         'id' => $v['id'],
				         'name' => $v['name'],
				         'sort' => $v['sort'],						 
				         'parent_id' => $v['parent_id'],
				         'str_pad' => $str_pad
			                          );
			       $temp = $this->get_list_category($array,$v['id'],($level+$add));
			       if($temp) 
				   {
				          $newarray = array_merge($newarray, $temp);
			       }
		      }
	       }
	       return $newarray;
    }

}


?>